<template>
	<view class="container">
		<!-- <uni-nav-bar fixed statusBar left-icon="left" backgroundColor="#ff9000" :border="false" color="#FFFFFF;">
		</uni-nav-bar> -->
		<view v-if="goods" class="title">
			<!-- <button @tap.stop="jump('/pages/shop/shop')">新增店铺</button> -->
			<view class="title-name">{{goods.name}}</view>
			<view class="title-box">
				<view class="title-date">{{$util.date("Y-m-d H:i",goods.createtime)}}</view>
				<view class="title-order-num">共{{total}}单</view>
			</view>
		</view>
		<scroll-view v-if="list && list.length > 0" class="box-list">
			<view class="card-box" v-for="(order,index) in list" :key="index"
				@tap.stop="jump('/pages/order/order-detail?id='+order.id)">
				<view class="card-box-head u-flex u-col-center">
					<view class="shop-name-box">
						<image mode="aspectFill" class="wm-type" style="margin-right: 20rpx;"
							:src="$IMG_URL + (order.wm_type == 1 ? '/imgs/freelunch/mt-icon.png' : order.wm_type == 2 ? '/imgs/freelunch/elm-icon.png' : '/imgs/freelunch/jd-icon.png')">
						</image>
						<text class="shop-name">{{order.wm_type == 1 ? '美团' : (order.wm_type == 2 ? '饿了么' : (order.wm_type == 3 ? '京东' : '未知'))}}平台</text>
					</view>
					<text class="order-status">{{index+1}}</text>
				</view>
				<view class="card-box-body card-box-body-cloumn">
					<!-- <view class="left-img">
						<image mode="aspectFill" :src="$IMG_URL+shop.logo"></image>
					</view> -->
					<view class="t-flex-col right-info">
						<view class="t-flex right-info-box right-info-type">
							<view class="right-info-box-icon">
								<!-- <image mode="aspectFill" :src="$IMG_URL + (shop.shop_type == 1 ? '/imgs/freelunch/mt-icon.png' : '/imgs/freelunch/elm-icon.png')">
								</image> -->
								外卖单号：
								{{order.proof[0].wm_order_sn}}
							</view>
							<!-- <view class="right-info-box-num">
							名额<text>1</text>/3
						</view> -->
							<view class="right-info-dec t-flex copy" @tap.stop="copy(order.proof[0].wm_order_sn)">复制
							</view>
						</view>
						<view class="t-flex right-info-box right-info-type">
							<view class="right-info-box-icon">
								<!-- <uni-icons style="margin-right: 10rpx;" type="calendar" size="22">
								</uni-icons> -->
								下单时间：{{order.place_time_text}}
							</view>
							<!-- <view class="right-info-box-dec">{{order.place_time_text}}</view> -->
						</view>
						<!-- <view class="t-flex right-info-box right-info-type">
							<view class="right-info-box-icon">
								店铺位置：
							</view>
							<view class="right-info-box-dec">123</view>
						</view> -->
					</view>
				</view>
				<view v-if="order.proof && order.proof[0]"
					class="t-flex card-box-footer u-flex u-col-center wm-img-box">
					<image @tap.stop="lookImg(order,0)" v-if="order.proof[0].one_image != ''" mode="aspectFill"
						:src="$IMG_URL+order.proof[0].one_image"></image>
					<image @tap.stop="lookImg(order,1)" v-if="order.proof[0].two_image != ''" mode="aspectFill"
						:src="$IMG_URL+order.proof[0].two_image"></image>
					<image @tap.stop="lookImg(order,2)" v-if="order.proof[0].three_image != ''" mode="aspectFill"
						:src="$IMG_URL+order.proof[0].three_image"></image>

					<!-- <view class="t-flex footer-status">


					</view>
					<view class="t-flex footer-btn" @tap.stop="jump('/pages/shop/shop-detail?id='+shop.id)">
						<button class="footer-btn-edit">查看详情</button>
					</view> -->
				</view>
			</view>
		</scroll-view>
		<view v-if="list && list.length > 0" class="sub">
			<button v-if="goods.is_pay == '0'" :loading="requestIng" :disabled="requestIng"
				@tap.stop="pay">立即支付</button>
			<button v-else>已支付</button>
		</view>
		<t-empty v-if="list.length == 0" tipText="空空如也~"></t-empty>
		<block v-if="selectId && selectId > 0">
			<t-menu></t-menu>
		</block>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapActions,
		mapState,
		mapGetters
	} from 'vuex';
	export default {
		data() {
			return {
				requestIng: false,
				id: 0,
				page: 1,
				goods: null,
				total: 0,
				list: [],
			};
		},
		computed: {

		},
		onLoad(e) {
			console.log('e', e)
			this.id = e.id;
			this.$store.dispatch('getShopList');
			this.getList()
		},
		methods: {
			jump(url, type = "navigateTo") {
				this.$util.jump(url, type)
			},
			lookImg(order, index) {
				let that = this;
				let imageList = [];
				if (order.proof?.length > 0) {
					let tem = order.proof[0]
					if (tem?.one_image) {
						imageList.push(this.$IMG_URL + tem.one_image)
					}
					if (tem?.two_image) {
						imageList.push(this.$IMG_URL + tem.two_image)
					}
					if (tem?.three_image) {
						imageList.push(this.$IMG_URL + tem.three_image)
					}
				}
				console.log('lo', order, index, imageList)
				uni.previewImage({
					current: index,
					urls: imageList,
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			copy(text) {
				let that = this;
				uni.setClipboardData({
					data: text,
					success: function() {
						that.$util.msg('复制成功');
					}
				});
			},
			getList() {
				let that = this;
				if (that.requestIng) return;
				that.requestIng = true;
				that.$util.loading('加载中');
				let requestData = {
					page: that.page,
					status: 3,
					goods_id: that.id
				};
				that.$http('order.list', requestData).then(res => {
						console.log('余额支付返回值', res)
						that.requestIng = false
						uni.hideLoading()
						if (res.code == 1) {
							that.goods = res.data.goods;
							that.total = res.data.total;
							if (requestData.page == 1) {
								that.list = res.data.list
							} else {
								that.list = that.list.concat(res.data.list)
							}
							if (res.data?.length > 0) that.page = requestData.page + 1
						} else {
							// resolve(false);
						}
					})
					.catch(err => {
						that.requestIng = false
						uni.hideLoading()
					})
			},
			pay() {
				console.log('confirm-goods')
				let that = this;
				!that.requestIng && ((that.$util.loading('发布中'), that.requestIng = true),
					that.$http('goods.pay', {
						id: that.id
					}).then(res => {
						console.log('余额支付返回值', res)
						that.requestIng = false
						uni.hideLoading()
						if (res.code == 1) {
							that.$util.msg(res.msg || '支付成功');
							setTimeout(() => {
								that.$store.dispatch('getUserInfo');
								// that.cance();
								//刷新页面数据
								// that.init()
								that.goods.is_pay == 1;
							}, 1000)
						} else if (res.code == -1) {
							that.$util.msg(res.msg || '余额不足');
							setTimeout(() => {
								that.$util.jump('/pages/user/wallet/recharge')
							}, 1000)
						} else if (res.code == 2) {
							//在线支付
							let data = res.data;
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: data.timeStamp,
								nonceStr: data.nonceStr,
								package: data.package,
								signType: data.signType,
								paySign: data.paySign,
								success: res => {
									console.log('支付成功', res)
									that.$util.msg(res.msg || '支付成功');
									setTimeout(() => {
										that.$store.dispatch('getUserInfo');
										// that.cance();
										//刷新页面数据
										// that.init()
										that.goods.is_pay == 1;
									}, 1000)
								},
								fail: function(err) {
									console.log('fail:', err);
									if (err.errMsg == 'requestPayment:fail cancel') {
										//取消支付
										that.$util.msg('取消支付');
									} else {
										that.$util.msg('支付失败');
									}
								}
							})
						} else {
							that.$util.msg(res.msg || '提交失败');
						}

					})
					.catch(err => {
						that.requestIng = false
						uni.hideLoading()
					}))
			},
		},
	}
</script>

<style lang="scss">
	.t-flex {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.t-flex-col {
		@extend .t-flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
	}

	.container {
		.box-list {
			margin: 160rpx 0 0;
		}

		.card-box {
			width: 94%;
			min-height: 100rpx;
			margin: 30rpx 3%;
			padding: 30rpx 0;
			background: #FFF;
			background-size: 100% 100%;
			box-shadow: 0px 7rpx 11rpx 2rpx rgba(213, 175, 97, 0.34);
			border-radius: 30rpx;
			overflow: hidden;
			position: relative;
			// z-index: 810;


			&-head,
			.card-box-footer {
				color: #000;
				font-size: 30rpx;
				display: flex;
				justify-content: space-between;
				width: 94%;
				margin-left: 3%;
				height: 50rpx;
				align-items: center;
			}

			&-head {
				.shop-name-box {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.wm-type {
						height: 60rpx;
						width: 60rpx;
					}
				}
			}

			.shop-name,
			.order-status {
				font-size: 30rpx;
				font-weight: 700;
			}

			.order-status {
				color: #ff9000;
			}

			&-body {
				width: 94%;
				margin: 30rpx 3%;
				display: flex;
				align-items: center;

				.left-img {
					width: 140rpx;
					height: 140rpx;
					border-radius: 20rpx;
					margin-right: 20rpx;
				}

				.right-info {
					width: 100%;
					font-size: 30rpx;
					color: #333;

					&-box {
						width: 100%;
						// justify-content: flex-start;
						justify-content: space-between;

						&-icon {
							@extend .t-flex;
							justify-content: center;
							height: 60rpx;
							min-width: 130rpx;
							margin-right: 10rpx;

							image {
								width: 38rpx;
								height: 38rpx;
							}
						}

						.right-info-box-num {
							font-size: 28rpx;
							color: #333;
							font-weight: 600;
							margin-right: 10rpx;

							text {
								color: #FF9000;
							}
						}

						.right-info-dec {
							color: #666;
						}
					}
				}
			}

			.card-box-footer {
				font-size: 28rpx;
				color: #666;

				.footer-status {
					.footer-status-view {
						@extend .t-flex;
						justify-content: flex-start;
						margin-right: 10rpx;

						.footer-status-text {
							color: #ff9000;
						}
					}
				}

				.footer-btn {
					button {
						height: 60rp;
						min-width: 140rpx;
						margin: 0 5rpx;
						border: none;
						background: #ff9000;
						border-radius: 80rpx;
						color: #fff;
						font-size: 26rpx;
					}

					.footer-btn-edit {
						background: #fff;
						border: 1px solid #ff9000;
						color: #ff9000;
					}
				}
			}

			.wm-img-box {
				height: auto;
				justify-content: flex-start;
				border-top: 1px solid #bbb;

				image {
					flex: 1;
					margin: 10rpx 20rpx 10rpx 0;
					height: 300rpx;
					border: 1px solid #eee;
					border-radius: 5px;
				}
			}

			/* .wm-img-box::after{
				position: absolute;
					right: 0;
					bottom: 0;
					left: 0;
					height: 1px;
					content: '';
					-webkit-transform: scaleY(.5);
					transform: scaleY(.5);
					background-color: red;
				
			} */
		}


	}

	image {
		width: 140rpx;
		height: 140rpx;
		border-radius: 10px;
	}

	.title {
		height: 160rpx;
		display: flex;
		flex-direction: column;
		// align-items: center;
		// justify-content: space-between;
		background: #ff9000;
		width: 100vw;
		box-sizing: border-box;
		padding: 30rpx 30rpx 20rpx;
		border-radius: 0 0 20px 20px;
		position: fixed;
		color: #fff;
		z-index: 800;
		left: 0;
		right: 0;
		top: 0;

		/* button {
			width: 160rpx;
			height: 70rpx;
			// border: 1px solid #fff;
			font-size: 24rpx;
			font-weight: 600;
			margin-right: 20rpx;
			border-radius: 70rpx;
			box-shadow: 0px 7rpx 11rpx 2rpx rgba(100, 100, 100, 0.7);
		} */
		.title-name {
			font-size: 34rpx;
			font-weight: 600;
		}



		.title-box {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 20rpx;
			font-size: 28rpx;
		}
	}

	.copy {
		font-size: 24rpx;
		color: #fff !important;
		background: #ff9000;
		padding: 4rpx 10rpx;
		margin-left: 10rpx;
		border-radius: 5px;
	}

	.sub {
		position: fixed;
		z-index: 800;
		background: #fff;
		height: 80rpx;
		/* #ifdef H5 */
		left: var(--window-left);
		right: var(--window-right);
		bottom: var(--window-bottom);
		/* #endif */
		/* #ifndef H5 */
		left: 0;
		right: 0;
		bottom: 0;
		/* #endif */
		padding: 20rpx 0;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);

		button {
			height: 100%;
			margin: 0 10%;
			border: none;
			background: #ff9000;
			border-radius: 80rpx;
			color: #fff;
		}

	}
</style>
